package com.beiing.tianshuai.tianshuai.huodong.model;

import com.beiing.tianshuai.tianshuai.dongtai.CallBack;
import com.beiing.tianshuai.tianshuai.entity.HuoDongCollectBean;
import com.beiing.tianshuai.tianshuai.entity.HuoDongItemBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by magikliu on 2017/7/21.
 */

public class HuoDongItemModel implements HuoDongItemModelImpl{

    private OnRequestListener mListener;

    public HuoDongItemModel(OnRequestListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void getHuoDong(String id, int firstRow, int listRows, final int type) {
        DisposableObserver<HuoDongItemBean> observer = new DisposableObserver<HuoDongItemBean>() {
            @Override
            public void onNext(@NonNull HuoDongItemBean bean) {
                mListener.onSuccess(bean, type);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                mListener.onError(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getHuoDongItem(id, firstRow, listRows)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void collectHuoDong(String uid, String aid, final CallBack callBack) {
        DisposableObserver<HuoDongCollectBean> observer = new DisposableObserver<HuoDongCollectBean>() {
            @Override
            public void onNext(@NonNull HuoDongCollectBean huoDongCollectBean) {
                callBack.result(huoDongCollectBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getHuoDongCollectResult(uid, aid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public interface OnRequestListener{

        void onSuccess(HuoDongItemBean bean, int type);
        void onError(Throwable throwable);
    }
}
